<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Controls actions related to the Expertise module of musashi
 *  
 * @package com_musashi
 * @author Daniel Torres
 */
 // Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access');

// Controller
jimport('joomla.application.component.controller');

// Make sure this user is a registered one. Otherwise, send him to the registration page
require_once(JPATH_COMPONENT.DS.'models'.DS.'user.php');

class MusashiControllerExpertise extends JController
{
    
    /**
     *  Makes sure this operation is valid by checking the validFormCheck flag.
     *  The flag is automatically reset upon call.
     */
    function isValidForm()
    {
        $session =& JFactory::getSession();
        $result = $session->get('validFormCheck',false);
        $session->set('validFormCheck',false);
        
        return $result;
    }
    
    
    /**
     *  Removes a song from this user's expertise table
     */
    function removeExpertise()
    {
        $model   =& $this->getModel('expertise');
        $songID  = JRequest::getInt('songid');
        $session =& JFactory::getSession();
                
        if( $model->removeSong($songID) == false )
        {
            $session->set('actionError','Failed to remove song');
        }
        
        // Go back to the expertise page
        $route = JRoute::_('index.php?option=com_musashi&view=expertise', false);
        $this->setRedirect($route, "");
        
    }
    
    /**
     *  Adds a new song to this user's expertise table
     */
    function addSong()
    {
            // Populate a new set of entries for this user's expertise, based on the selected song
            $model   =& $this->getModel('expertise');
            $songID  =  JRequest::getInt('songid');
            $session =& JFactory::getSession();
            if( $model->addNewSong( $songID ) )
            {
                $session->set('lastUpdated',$songID);
            }
            else
            {
                $session->set('actionError','Error updating song information');                
            }
            
            
            // Go back to the expertise page
            $route = JRoute::_('index.php?option=com_musashi&controller=expertise&view=expertise', false);
            $this->setRedirect($route, "");

    }
    
    /**
     *  Updates the expertise of a given song
     */
    function updateSongExpertise()
    {
        $model   =& $this->getModel('expertise');
        $session =& JFactory::getSession();
        $songID  =  JRequest::getInt('songid');
        
        if(  $model->updateSongExpertise() )
        {
           $session->set('lastUpdated',$songID);
           $session->set('actionResult','Information updated!');
        }
        else
        {
            $session->set('actionError','Error updating song information');
        }
        
        // Go back
        $route = JRoute::_('index.php?option=com_musashi&controller=expertise&view=expertise',false);
        $this->setRedirect($route,"");
    }
    
    /**
     * Displays the main Expertise
     * @access public
     */
    function display()
    {
        // Make sure we're registered  users
        if( MusashiModelUser::validateUser( 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] ) == false )
        {
            // Redirect to registration view
            $url = MusashiModelUser::getUnregisteredUserPath();
            $this->setRedirect( $url );            
        }

        parent::display();
    }       
}

?>